<!-- 考勤编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改日考勤配置':'设置日考勤配置'"
    :visible="visible"
    v-if="visible"
    width="850px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-row :gutter="10">
        <el-steps :active="active" finish-status="success" style="margin:30px 100px">
          <el-step title="创建考勤规则"></el-step>
          <el-step title="设置考勤场地"></el-step>
        </el-steps>

      </el-row>
      <div v-if="step==1">
      <el-row :gutter="15">
      <el-col :sm="12">
      <el-form-item label="所属学校:" prop="organization">
          <el-select
            filterable
            clearable
            v-model="form.organization"
            @change="getGradeList"
            disabled
            size="small"
            placeholder="-请选择所属学校-"
            class="ele-block">
            <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>
      </el-col>
      <el-col :sm="12">
      <el-form-item label="所属年级:" prop="grade">
          <el-select
            filterable
            clearable
            v-model="form.grade"
            @change="getClassList"
            disabled
            size="small"
            placeholder="-请选择所属年级-"
            class="ele-block">
            <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>
      </el-col>
      </el-row>
       <el-row :gutter="15">
         <el-col :sm="12">
         <el-form-item label="所属班级:" prop="clazz">
             <el-select
               filterable
               clearable
               v-model="form.clazz"
               @change="$forceUpdate()"
               disabled
               size="small"
               placeholder="-请选择班级-"
               class="ele-block">
               <el-option v-for="item in clazzList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
         </el-form-item>
         </el-col>
      </el-row>
      <el-row :gutter="15">
      <el-form-item label="考勤模式:" prop="model">
        <el-radio-group
          v-model="form.model">
          <el-radio :label="1">单次</el-radio>
          <el-radio :label="2">按周循环</el-radio>
        </el-radio-group>
      </el-form-item>
      </el-row>
      <el-row :gutter="15">
      <el-form-item label="考勤循环:" prop="cycle">
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        <el-checkbox-group
         v-model="form.cycle" @change="handleCheckedCitiesChange">
        <el-checkbox v-for="xx in xq" :label="xx" :key="xx">{{xx}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      </el-row>
      <el-form-item label="考勤设置:" prop="checkin">
      <el-row justify="center" align="middle" style="height: 2vw;">
          <el-col style="text-align: center;" :span="2">
          考勤序号
          </el-col>
          <el-col style="text-align: center;" :span="3">
            考勤时间
          </el-col>
          <el-col style="text-align: center;" :span="3">
            签到/签退
          </el-col>
          <el-col style="text-align: center;" :span="12">
            打卡时间范围
          </el-col>
          <el-col style="text-align: center;" :span="2">
          操作
          </el-col>
      </el-row>
      <el-row :gutter="15"
       v-for="(item, index) in ruleForms"
              :key="index"
              style="margin-left: 0.5vw">
          <el-col :span="2">
            <el-form-item label-width="0">
              {{index+1}}
              </el-form-item>
            </el-col>
          <el-col :span="3">
          <el-form-item label-width="0">
            <el-time-select
              v-model="item.kqtime"
              :picker-options="{
                  start: '06:30',
                  step: '00:05',
                  end: '23:30'
                }"
              placeholder="选择时间"
              style="width: 90px;">
            </el-time-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label-width="0">
              <el-select
                 v-model="item.qian"
                 placeholder="请选择"
                 >
                  <el-option label="签到" value="qd"></el-option>
                  <el-option label="签退" value="qt"></el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label-width="0">
              <el-time-picker
                is-range
                :editable="false"
                v-model="item.start_end"
                format="HH:mm"
                value-format="HH:mm"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围">
              </el-time-picker>
            </el-form-item>
        </el-col>

        <el-col :span="2">
              <i
                 class="el-icon-circle-plus-outline"
                 type="danger"
                 v-if="index == 0"
                 style="font-size: 30px; color: #67c23a; margin-right: 5px"
                 @click="addItem"
                 ></i>
              <i
                 class="el-icon-remove-outline"
                 type="danger"
                 v-if="index != 0"
                 style="font-size: 30px; color: #f56c6c"
                 @click="del(index)"
                 ></i>
          </el-col>
      </el-row>
      </el-form-item>
      </div>
      <div v-if="step==2">
        <el-row :gutter="15">
        <el-form-item label="考勤方式:" prop="way">
          <el-radio-group
            v-model="form.way"
            @change="wayChange">
            <el-radio :label="1">按场地选</el-radio>
            <el-radio :label="2">按设备选</el-radio>
          </el-radio-group>
        </el-form-item>
        </el-row>
        <el-row :gutter="15">
        <el-form-item label="" ref="sceneid" prop="sceneid">
            <el-select
              filterable
              clearable
              v-model="form.sceneid"
              @change="$forceUpdate()"
              size="small"
              placeholder="-请选择-"
              class="ele-block">
              <el-option v-for="item in sceneList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
        </el-form-item>
        </el-row>
      <el-row :gutter="15">
      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
      </el-row>
      <el-row :gutter="15">
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
      </el-form-item>

      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
      </el-row>
      </div>

          </el-form>
    <div slot="footer">
      <el-button v-if="step==2" style="margin-top: 12px;" @click="prev">上一步</el-button>
      <el-button v-if="step==2" @click="updateVisible(false)">取消</el-button>
      <el-button v-if="step==2"
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
      <el-button v-if="step==1" style="margin-top: 12px;" @click="next">下一步</el-button>
    </div>
  </el-dialog>
</template>

<script>

const xqoptions = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];
export default {
  name: 'StandardEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: {},
      step: 1,
      stalevel:[],
      active: 0,
      checkAll: false,
      xq: xqoptions,
      isIndeterminate: false,
      // 等级数据1

      // 等级数据2

      // 表单验证规则
      rules: {
              organization: [
                {required: true, message: '请输入所属学校', trigger: 'blur'}
              ],
              grade: [
                {required: true, message: '请输入所属年级', trigger: 'blur'}
              ],
              clazz: [
                {required: true, message: '请输入所属班级', trigger: 'blur'}
              ],
              model: [
                {required: true, message: '请选择考勤模式', trigger: 'blur'}
              ],
              cycle: [
                {required: true, message: '请选择考勤循环', trigger: 'blur'}
              ],
              checkin: [
                {required: true, message: '请输入考勤设置', trigger: 'blur'}
              ],
              way: [
                {required: true, message: '请选择考勤方式', trigger: 'blur'}
              ],
               sceneid: [
                {required: true, message: '请选择场景', trigger: 'blur'}
              ],
              sort: [
                {required: true, message: '请输入显示顺序', trigger: 'blur'}
              ],
              note: [
                {required: true, message: '请输入备注', trigger: 'blur'}
              ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学院列表
      schoolList: [],
      // 年级列表
      gradeList: [],
      // 班级列表
      clazzList: [],
      sceneList: [],
      ruleForms:[{kqtime:'',qian: '',start_end:['06:00','20:00']}]
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({organization:parseInt(this.data.organization),grade:parseInt(this.data.grade),clazz:parseInt(this.data.id),cycle:[]}, this.data.attendance);
        this.getSchoolList();
        if(this.data.attendance&&this.data.attendance.checkin){
          this.ruleForms = this.data.attendance.checkin;
        }else{
          this.ruleForms = [{kqtime:'',qian: '',start_end:['06:00','20:00']}];
        }
        if(this.form.way==1){
            this.getSceneList();
        }
        if(this.form.way==2){
            this.getEquipList();
        }
        // console.log(this.ruleForms);
        this.isUpdate = true;
      } else {
        this.form = {cycle:[]};
        this.ruleForms = [{kqtime:'',qian: '',start_end:['06:00','20:00']}];
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取学院列表
    this.getSchoolList();

    // 获取设备列表
    // this.getSceneList();
  },
  methods: {
    next() {
            if (this.active++ > 2) this.active = 0;this.step=2;this.$forceUpdate();
          },
    prev() {
            if (this.active-- > 0) this.active = 0;this.step=1;this.$forceUpdate();
          },
    wayChange(){
      this.form.sceneid = '';
      this.$forceUpdate();
      if(this.form.way==1){
          this.getSceneList();
      }
      if(this.form.way==2){
          this.getEquipList();
      }
    },
    handleCheckAllChange(val) {
      this.form.cycle = val ? xqoptions : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.xq.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.xq.length;
    },
    addItem() {
        // this.i += 1;
        this.ruleForms.push({sex:'',conditions: '',target:['',''],unit:'',level:'',score:''});
    },
    // 删除
    del(index) {
        this.ruleForms.splice(index, 1);
    },
    // 修改单位
    changelevel(index){
      if (index=='kg/m^2') {
        this.stalevel = this.stalevel2;
      }else{
        this.stalevel = this.stalevel1
      }
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.cycle = JSON.stringify(this.form.cycle);
          this.form.checkin = JSON.stringify(this.ruleForms);
          this.$http.post('/attendance/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取组织列表
     */
    getSchoolList() {
      if(this.form.organization){
        this.$forceUpdate();
        this.getGradeList();
      }
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取运动项目列表
     */
    getGradeList() {
      if(this.form.grade){
        this.$forceUpdate();
        this.getClassList();
      }
      this.$http.post('/grade/getGradeList',{oid:this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.gradeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取班级列表
     */
    getClassList() {
      if(this.form.clazz){
        this.$forceUpdate();
      }
      this.$http.post('/clazz/getClazzList',{gid:this.form.grade}).then(res => {
        if (res.data.code === 0) {
          this.clazzList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取场地列表
     */
    getSceneList() {
      if(this.form.sceneid){
        this.$forceUpdate();
      }
      this.$http.post('/sitescene/getSceneList',{}).then(res => {
        if (res.data.code === 0) {
          this.sceneList = res.data.data;
          this.$forceUpdate();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取设备列表
     */
    getEquipList() {
      if(this.form.sceneid){
        this.$forceUpdate();
      }
      this.$http.post('/equip/getEquipList',{}).then(res => {
        if (res.data.code === 0) {
          this.sceneList = res.data.data;
          this.$forceUpdate();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
